package Z11_3_moreLock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author： pang-yun
 * @date： 2021-11-03 11:35
 * 功能目的 ：用于 多把锁
 */

public class MultiLock {
    public static void main(String[] args) {
        BigRoom bigRoom = new BigRoom();

        // 一把锁   只能串行
 /*       new Thread(() -> {
            bigRoom.sleep();
        }, "dog").start();
        new Thread(() -> {
            bigRoom.study();
        }, "cat").start();
        */


        ModifyRoom modifyRoom = new ModifyRoom();
        // 每个房间 一把锁  并行
        new Thread(()->{
            modifyRoom.sleep();
        },"sleepRoom").start();
        new Thread(()->{
            modifyRoom.study();
        },"studyRoom").start();




    }
}


@Slf4j
class BigRoom {

    public void sleep() {
        synchronized (this) {
            log.info("sleep  two hours");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void study() {
        synchronized (this) {
            log.info("study one hours");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

@Slf4j
class ModifyRoom {
    private final Object SLEEP_ROOM = new Object();
    private final Object STUDY_ROOM = new Object();

    public void sleep(){
        synchronized (SLEEP_ROOM){
            log.info("sleep three hours.....");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void study(){
        synchronized (STUDY_ROOM){
            log.info("study three hours....");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}